package com.example.miniblog.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.miniblog.entity.LoginForm;
import com.example.miniblog.entity.User;
import com.example.miniblog.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public  class UserService implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean add(User user) {
        int i = userMapper.insert(user);
        return (i==1)?true:false;
    }

    @Override
    public boolean removeUser(String id) {
        int i = userMapper.deleteById(id);
        return (i==0)?false:true;
    }

    @Override
    public boolean updateUser(User user) {
        int i = userMapper.updateById(user);
        return (i==0)?false:true;
    }

    @Override
    public boolean logout(User user) {
        return true;
    }

    @Override
    public User getUserById(String id) {
        return userMapper.selectById(id);
    }

    @Override
    public List<User> getAll() {
        List<User> users = userMapper.selectList(null);
        return users;
    }

    public boolean login(LoginForm loginForm) {
        QueryWrapper<User>  queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("username",loginForm.getUsername());
        queryWrapper.eq("password",loginForm.getPassword());
        User user = userMapper.selectOne(queryWrapper);
        if(user == null){
            return false;
        }else {
            return true;
        }
    }
}
